# -*- coding: utf8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.cvm.v20170312 import cvm_client, models

import json

# 设置一次请求最高的服务器数量，避免耗尽资金
MAX_NEW_CVM_COUNT = 100

IS_DEBUG = True

with open('config/config.json', 'r') as f:
    myConfig = json.load(f)
with open('config/instances.json', 'r') as f:
    myInstances = json.load(f)

try:
    cred = credential.Credential(myConfig['SecretId'], myConfig['SecretKey'])
    # 注意 region 需要定义
    client = cvm_client.CvmClient(cred, myConfig['Region'])

    # 查询 IP 地址等
    req = models.DescribeInstancesRequest()
    req.InstanceIds = myInstances
    req.Limit = 100
    ret = client.DescribeInstances(req)
    if IS_DEBUG:
        print("[DEBUG]", ret.to_json_string())
    print("私网 IP 地址:")
    for instance in ret.InstanceSet:
        for ipaddr in instance.PrivateIpAddresses:
            print(ipaddr)
    print()

except TencentCloudSDKException as err:
    print(err)
    exit(1)

except Exception as e:
    import traceback

    print('traceback.format_exc():\n%s' % traceback.format_exc())
    exit(1)
